package com.ghh.encrypto;

import java.security.AccessControlException;
import java.security.KeyStore;
import java.security.KeyStore.SecretKeyEntry;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableEntryException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.SecretKey;

import oracle.security.jps.JpsContext;
import oracle.security.jps.JpsContextFactory;
import oracle.security.jps.JpsException;
import oracle.security.jps.internal.policystore.JavaPolicyProvider;
import oracle.security.jps.service.keystore.KeyStoreService;
import oracle.security.jps.service.keystore.KeyStoreServiceException;

public class FBLdiKeyStoreService {
	private static final String	stripName			= "FBLdi";
	private static final String	keystoreName		= "ks1";
	private static final String	keystorePassword	= "keyStore1234";
	private static final String	keyAlias			= "secretKeyAlias";

	static {
		System.setProperty("oracle.security.jps.config", "./jps-config-jse.xml");
		java.security.Policy.setPolicy(new JavaPolicyProvider());
	}

	private KeyStoreService		kss					= null;

	public FBLdiKeyStoreService() throws JpsException {
		JpsContext ctx = JpsContextFactory.getContextFactory().getContext();
		kss = ctx.getServiceInstance(KeyStoreService.class);
	}

	public void saveSecretKey(SecretKey key) throws KeyStoreServiceException, AccessControlException, KeyStoreException {
		KeyStore ks = loadKeyStore();
		KeyStore.SecretKeyEntry skEntry = new KeyStore.SecretKeyEntry(key);
		ks.setEntry(keyAlias, skEntry, new KeyStore.PasswordProtection(keystorePassword.toCharArray()));
		kss.persist();
	}

	public SecretKey getSecretKey() throws KeyStoreServiceException, AccessControlException, NoSuchAlgorithmException,
			UnrecoverableEntryException, KeyStoreException, InvalidKeySpecException {
		if (!existKeyStore()) {
			return null;
		}
		KeyStore ks = kss.getKeyStore(stripName, keystoreName, null);
		KeyStore.SecretKeyEntry entry = (SecretKeyEntry) ks.getEntry(keyAlias, new KeyStore.PasswordProtection(
				keystorePassword.toCharArray()));
		return entry.getSecretKey();
	}

	// public void doKeyStoreOperation() {
	// doOperation();
	// }
	//
	// public void doPrivilegedKeyStoreOperation() {
	// AccessController.doPrivileged(new PrivilegedAction<String>() {
	// public String run() {
	// doOperation();
	// return "done";
	// }
	// });
	// }
	//
	// private void doOperation() {
	// try {
	// kss.createKeyStore("keystoreapp", "ks1", null, null);
	// } catch (KeyStoreServiceException e) {
	// e.printStackTrace();
	// }
	// }

	private void createKeyStore() throws KeyStoreServiceException, AccessControlException {
		kss.createKeyStore(stripName, keystoreName, null, null);
	}

	private boolean existKeyStore() throws KeyStoreServiceException, AccessControlException {
		String[] keystores = kss.listKeyStores(stripName);
		if (keystores == null || keystores.length == 0) {
			return false;
		} else {
			boolean b = false;
			for (String k : keystores) {
				if (keystoreName.equals(k)) {
					b = true;
					break;
				}
			}
			return b;
		}
	}

	private KeyStore loadKeyStore() throws KeyStoreServiceException, AccessControlException {
		if (!existKeyStore()) {
			createKeyStore();
		}
		return kss.getKeyStore(stripName, keystoreName, null);
	}

}
